﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MCDirectBroadcast.Entity;

namespace MCDirectBroadcast.Pages
{
	/// <summary>
	/// Interaction logic for Home2.xaml
	/// </summary>
	public partial class Home2 : Window
	{
		private MCDierectServer Server;

		public Story SelectedStory { get; set; }

		public Home2()
		{
			InitializeComponent();
			InitEvents();
		}

		void InitEvents()
		{
			cmdGoOnline.Click += new RoutedEventHandler(CmdGoOnline_Click);

			BtnAdd.Click += new RoutedEventHandler(BtnAdd_Click);
			BtnEdit.Click += new RoutedEventHandler(BtnEdit_Click);
			BtnDelete.Click += new RoutedEventHandler(BtnDelete_Click);

			this.Loaded += new RoutedEventHandler(Home2_Loaded);
			StryList.SelectChanged += new SelectionChangedEventHandler(StryList_SelectChanged);
		}

		void CmdGoOnline_Click(object sender, RoutedEventArgs e)
		{
			if (Server == null)
			{
				Server = new MCDierectServer(2526);
				Server.HostOn += new HostActionHandler(Server_HostOn);
				Server.HostOff += new HostActionHandler(Server_HostOff);

				Server.StartHosting();
			}
			else
			{
				Server.StopHosting();
				Server = null;
			}
		}

		void Server_HostOff(object sender, HostActionEventArgs e)
		{
			txbOnlineStatus.Text = "Offline";
			cmdGoOnline.Header = "Go Online";
		}

		void Server_HostOn(object sender, HostActionEventArgs e)
		{
			txbOnlineStatus.Text = "Online";
			cmdGoOnline.Header = "Go Offline";
		}

		void StryList_SelectChanged(object sender, SelectionChangedEventArgs e)
		{
			if (e.AddedItems.Count > 0)
			{
				Story s = e.AddedItems[0] as Story;
				if (s != null)
				{
					StryView.Fill(s);
					SelectedStory = s;
				}
			}
			else
				SelectedStory = null;
		}

		void Home2_Loaded(object sender, RoutedEventArgs e)
		{
			StryList.Reload();

			MemberList.Refresh();
		}

		void BtnDelete_Click(object sender, RoutedEventArgs e)
		{
			MessageBoxResult rs = MessageBox.Show(this, 
				"Are you sure to delete selected item?",
				"Story delete confermation", 
				MessageBoxButton.YesNo, 
				MessageBoxImage.Question);

			if (rs == MessageBoxResult.No)
				return;

			if (SelectedStory != null)
			{
				try
				{
					SQLCeHelper.UpdateStory(SelectedStory);
					StryList.Reload();
				}
				catch
				{
					MessageBox.Show("Unable to perform deletion due to DB's issues.");
				}
			}
			else
			{
				MessageBox.Show("Please select item one to be deleted.");
			}
		}

		void BtnEdit_Click(object sender, RoutedEventArgs e)
		{
			if (SelectedStory != null)
			{
				AddEditStory form = new AddEditStory();
				form.StoryItem = SelectedStory;
				form.ShowDialog();

				StryList.Reload();
			}
			else
			{
				MessageBox.Show("Please select item one to be deleted.");
			}
		}

		void BtnAdd_Click(object sender, RoutedEventArgs e)
		{
			new AddEditStory().ShowDialog();
			StryList.Reload();
		}
	}
}
